<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class CarAuth
 * @package App\Models
 * @version December 22, 2017, 4:19 am UTC
 *
 * @property string park_code
 * @property string username
 * @property string cardo
 * @property int type
 * @property string vpl_number
 * @property string mobile
 * @property string address
 * @property timestamp validbtime
 * @property timestamp validatime
 * @property string operator
 * @property string email
 * @property string share
 * @property int share_count
 * @property string memo
 * @property string serial_no
 * @property int priority
 * @property string recharge
 * @property string extra_data
 */
class CarAuthLog extends Model
{
    use SoftDeletes;

    public $table = 'carauths_log';
    

    protected $dates = ['deleted_at'];


    public $fillable = [
        'park_code',
        'username',
        'cardno',
        'type',
        'vpl_number',
        'mobile',
        'address',
        'validbtime',
        'validetime',
        'operator_id',
        'operator',
        'email',
        'share',
        'share_count',
        'memo',
        'serial_no',
        'priority',
        'recharge',
        'extra_data',
        'auth_type',
        'belong_org',
        'images',
        'down_status',
        'auth_type',
        'option',
        'issued_status',
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'park_code' => 'string',
        'username' => 'string',
        'cardno' => 'string',
        'vpl_number' => 'string',
        'mobile' => 'string',
        'address' => 'string',
        'operator' => 'string',
        'email' => 'string',
        'share' => 'string',
        'memo' => 'string',
        'serial_no' => 'string',
        'recharge' => 'string',
        'extra_data' => 'string'
    ];

    public function organization()
    {
        return $this->belongsTo(Organization::class,'belong_org');
    }
    public function operators()
    {
        return $this->belongsTo(User::class,'operator_id');
    }

    /**
     * 将资源转换成数组。
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray()
    {
        $mergeArray = [
            'organizations'=>[],
        ];

        if(!is_null($this->organization)) {
            $mergeArray['organizations'] = Organization::ancestorsAndSelf($this->belong_org);
        }

        return array_merge(parent::toArray(), $mergeArray);
    }




}
